package demo;

import java.util.TreeMap;

//Day08
public class Day18003 {
    public static void main(String[] args) {
        TreeMap<Person02,String> map=new TreeMap<>();
        map.put(new Person02("张三",18),"北京");
        map.put(new Person02("李四",20),"上海");
        map.put(new Person02("王五",35),"天津");
        map.put(new Person02("赵六",21),"北京");
        map.put(new Person02("田七",19),"上海");

        System.out.println(map);
//        删除张三的信息
        for (Person02 p:map.keySet()) {
            if(p.getName().equals("张三")){
                map.remove(p);
                break;
            }
        }
        System.out.println(map);
//        修改李四的家庭住址
        for (Person02 p:map.keySet()) {
            if(p.getName().equals("李四")){
                map.put(p,"郑州");
                break;
            }
        }
        System.out.println(map);
//        两种方式遍历
        for (Person02 p:
             map.keySet()) {
            System.out.println(p+"---"+map.get(p));
        }

        map.forEach((key,value)->{
            System.out.println(key+"---"+value);
        });
    }
}
//劳模类
class Person02 implements Comparable<Person02>{
    private String name;
    private Integer age;

    public Person02() {
    }

    public Person02(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person02{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person02 person02) {
        int result=this.getAge()-person02.getAge();
        if(result==0)
            result=this.getName().compareTo(person02.getName());
        return result;
    }
}
